<?php

namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Model\Order;
use App\Model\Customer;
use App\Model\Route;
use App\Model\Biaoqian;
use App\Model\Deal;
use App\Model\Bumen;
use App\Model\User;
class OrderController extends Controller
{
    /**
     * 
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    { 
        if(session('wechat.role')){
            foreach(session('wechat.role') as $vo){
                if($vo=="总经理"){
                    $order=Order::all();
                    foreach($order as $k=>$vo){
                        $customer=Customer::where('id',$vo->order_customerid)->first()->customer_name;
                        $type=Route::where('id',$vo->order_didian)->first()->title;
                        $didian=Route::where('id',$vo->order_didian2)->first()->title;
                        $biaoqian=Biaoqian::where('id',$vo->order_lvyouleibie)->first()->name;
                        $order[$k]['customer']=$customer;
                        $order[$k]['type']=$type;
                        $order[$k]['didian']=$didian;
                        $order[$k]['biaoqian']=$biaoqian;
                        if($vo->order_qufen==2){
                            $order[$k]['dan']="成交单";
                            $hesuan=['1'=>'未核算','2'=>'核算中','3'=>'已核算'];
                            $tai=$hesuan[Deal::where('deal_orderid',$vo->id)->first()->deal_jiesuanstatue];
                            $order[$k]['tai']=$tai;
                        }else{
                             $order[$k]['dan']="意向单";
                             $yixiang=['1'=>'商谈','3'=>'失败','4'=>'成功'];
                             $tai=$yixiang[$vo->order_statue];
                             $order[$k]['tai']=$tai;
                        }
                    }
                }elseif($vo=="经理"){
                    $bumen=session('wechat.userid');
                    $bumen=\DB::table('admin_user_permissions')->where('user_id',$bumen)->first()->permission_id;
                    $bumen=\DB::table('admin_user_permissions')->where('permission_id',$bumen)->pluck('user_id');
                    $order=Order::whereIn('staff_id',$bumen)->orderBy('id', 'desc')->get();
                    foreach($order as $k=>$vo){
                        $customer=Customer::where('id',$vo->order_customerid)->first()->customer_name;
                        $type=Route::where('id',$vo->order_didian)->first()->title;
                        $didian=Route::where('id',$vo->order_didian2)->first()->title;
                        $biaoqian=Biaoqian::where('id',$vo->order_lvyouleibie)->first()->name;
                        $order[$k]['customer']=$customer;
                        $order[$k]['type']=$type;
                        $order[$k]['didian']=$didian;
                        $order[$k]['biaoqian']=$biaoqian;
                        if($vo->order_qufen==2){
                            $order[$k]['dan']="成交单";
                            $hesuan=['1'=>'未核算','2'=>'核算中','3'=>'已核算'];
                            $tai=$hesuan[Deal::where('deal_orderid',$vo->id)->first()->deal_jiesuanstatue];
                            $order[$k]['tai']=$tai;
                        }else{
                             $order[$k]['dan']="意向单";
                             $yixiang=['1'=>'商谈','3'=>'失败','4'=>'成功'];
                             $tai=$yixiang[$vo->order_statue];
                             $order[$k]['tai']=$tai;
                        }
                    }
                }else{
                    $order=Order::where('staff_id',session('wechat.userid'))->orderBy('id','desc')->get();
                    foreach($order as $k=>$vo){
                        $customer=Customer::where('id',$vo->order_customerid)->first()->customer_name;
                        $type=Route::where('id',$vo->order_didian)->first()->title;
                        $didian=Route::where('id',$vo->order_didian2)->first()->title;
                        $biaoqian=Biaoqian::where('id',$vo->order_lvyouleibie)->first()->name;
                        $order[$k]['customer']=$customer;
                        $order[$k]['type']=$type;
                        $order[$k]['didian']=$didian;
                        $order[$k]['biaoqian']=$biaoqian;
                        if($vo->order_qufen==2){
                            $order[$k]['dan']="成交单";
                            $hesuan=['1'=>'未核算','2'=>'核算中','3'=>'已核算'];
                            $tai=$hesuan[Deal::where('deal_orderid',$vo->id)->first()->deal_jiesuanstatue];
                            $order[$k]['tai']=$tai;
                        }else{
                             $order[$k]['dan']="意向单";
                             $yixiang=['1'=>'商谈','3'=>'失败','4'=>'成功'];
                             $tai=$yixiang[$vo->order_statue];
                             $order[$k]['tai']=$tai;
                        }
                    }
                }
            }
            $shuliang=10;
            $order=$order->take(10);
            $type=Route::where('parent_id',0)->get();
            // $didian=Route::where('parent_id',Route::where('parent_id',0)->first()->id)->get();
            $bumen=Bumen::all();
            $biaoqian=Biaoqian::all();
            return view('order.index',['list'=>$order,'type'=>$type,'bumen'=>$bumen,'shuliang'=>$shuliang,'biaoqian'=>$biaoqian]);
        }else{
            return redirect(url('/'));
        }
        
    }
    // 表单筛选查询
    public function chaxun(Request $request)
    { 
        $list=json_decode($request['data']);
        if(session('wechat.role')){
            foreach(session('wechat.role') as $vo){
                if($vo=="总经理"){
                    $order=Order::all();
                }elseif($vo=="经理"){
                    $bumen=session('wechat.userid');
                    $bumen=\DB::table('admin_user_permissions')->where('user_id',$bumen)->first()->permission_id;
                    $bumen=\DB::table('admin_user_permissions')->where('permission_id',$bumen)->pluck('user_id');
                    $order=Order::whereIn('staff_id',$bumen)->orderBy('id', 'desc')->get();
                }else{
                    $order=Order::where('staff_id',session('wechat.userid'))->orderBy('id','desc')->get();
                }
                if($list->bumen){
                    if($list->bumen!="请选择部门"){
                        $bumen=Bumen::where('name',$list->bumen)->first()->id;
                        $bumenuser=\DB::table('admin_user_permissions')->where('permission_id',$bumen)->pluck('user_id');
                        $order=$order->whereIn('staff_id',$bumenuser);
                    }
                }
                if($list->yuangong){
                    $yuangong=User::where('username','like','%'.$list->yuangong.'%')->pluck('id');
                    $order=$order->whereIn('staff_id',$yuangong);
                }
                if($list->customer){
                    $cus=Customer::where('customer_name','like','%'.$list->customer.'%')->pluck('id');
                    $order=$order->whereIn('order_customerid',$cus);
                }
                if($list->type){
                    if($list->type!='请选择旅游类型'){
                        $type=Route::where('title',$list->type)->first()->id;
                        $order=$order->where('order_didian',$type);
                    }
                }
                if($list->didian){
                    if($list->didian!='请选择旅游地点'){
                        $type=Route::where('title',$list->didian)->first()->id;
                        $order=$order->where('order_didian2',$type);
                    }
                }
                if($list->biaoqian){
                    $biaoqian=Biaoqian::where('name',$list->biaoqian)->first()->id;
                    $order=$order->where('order_lvyouleibie',$biaoqian);
                }
                if($list->start_date){
                    $order_orderdate=$list->start_date." 00:00:00";
                    $order_orderdate=substr($order_orderdate, 2);
                    $order=$order->where('order_orderdate','>',$order_orderdate);
                }
                if($list->end_date){
                    $order_orderdate=$list->end_date." 00:00:00";
                    $order_orderdate=substr($order_orderdate, 2);
                    $order=$order->where('order_orderdate','<',$order_orderdate);
                }
                if($list->zhuangtai){
                    $zhuangtai=$list->zhuangtai;
                    if($zhuangtai=="意向单"){
                        $order=$order->where('order_qufen',1);
                    }else{
                        $order=$order->where('order_qufen',2);
                    }
                }
                foreach($order as $k=>$vo){
                    $customer=Customer::where('id',$vo->order_customerid)->first()->customer_name;
                    $type=Route::where('id',$vo->order_didian)->first()->title;
                    $didian=Route::where('id',$vo->order_didian2)->first()->title;
                    $biaoqian=Biaoqian::where('id',$vo->order_lvyouleibie)->first()->name;
                    $order[$k]['customer']=$customer;
                    $order[$k]['type']=$type;
                    $order[$k]['didian']=$didian;
                    $order[$k]['biaoqian']=$biaoqian;
                    if($vo->order_qufen==2){
                        $order[$k]['dan']="成交单";
                        $hesuan=['1'=>'未核算','2'=>'核算中','3'=>'已核算'];
                        $tai=$hesuan[Deal::where('deal_orderid',$vo->id)->first()->deal_jiesuanstatue];
                        $order[$k]['tai']=$tai;
                    }else{
                         $order[$k]['dan']="意向单";
                         $yixiang=['1'=>'商谈','3'=>'失败','4'=>'成功'];
                         $tai=$yixiang[$vo->order_statue];
                         $order[$k]['tai']=$tai;
                    }
                } 
                $order=$order->take($list->shuliang);
                $dd['order']=$order;
                $dd['shuliang']=$list->shuliang;
                return json_encode($dd);
            }
        }else{
            return json_encode('false');
        }
    }
    /**
     * 进行客户搜索
     *
     * @return \Illuminate\Http\Response
     */
    public function lvyouleixing()
    {
        $leixing=$_GET['lvyouleixing'];
        if($leixing!=''){
            $lvyouleixing=Route::where('title',$leixing)->first()->id;
            $didian=Route::where('parent_id',$lvyouleixing)->pluck('title');
            return json_encode($didian);
        }else{
            return json_encode('');
        }
    }
    public function create()
    {
        
    }
   
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
